package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.QuestionSeckillDAO;
import com.oc.mba.entity.OcQuestionSeckill;
import com.oc.mba.entity.OcQuestionSolution;
import com.oc.mba.util.CommLog;

@Repository
public class QuestionSeckillDAOImpl extends BaseDAOHibernate implements QuestionSeckillDAO {
	private static final CommLog logger = new CommLog(QuestionSeckillDAOImpl.class);

	public List<OcQuestionSeckill> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcQuestionSeckill instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from OcQuestionSeckill as model where model."
					+ propertyName + "= ? order by model.id";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public void save(OcQuestionSeckill seckill) {
		getHibernateTemplate().save(seckill);
	}
	
	@Override
	public OcQuestionSeckill findQuestionSeckill(Integer seckillID) {
		List<OcQuestionSeckill> seckillList = findByProperty("id", seckillID);
		if (seckillList != null && seckillList.size() > 0) {
			return seckillList.get(0);
		}
		return null;
	}

	@Override
	public void deleteSeckill(Integer seckillID) {
		OcQuestionSeckill seckill = findQuestionSeckill(seckillID);
		if (seckill != null) {
			getHibernateTemplate().delete(seckill);
		}
	}
	
}
